﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Newtonsoft.Json;

namespace Sales
{
    public partial class frmHycz : Form
    {
        public SysConfig config;
        public static string config_dir = Application.StartupPath + "/config";
        public static string config_file = config_dir + "/config.xml";
        public Common2 common = new Common2();

        public frmHycz()
        {
            config = Common2.GetConfig();//获取相关配置属性
            InitializeComponent();
            MiddleModule.eventSend += new Send(MiddleModule_eventSend);

        }
        #region 页面属性
        /// <summary>
        /// 卡号
        /// </summary>
        public string Kh
        {
            get;
            set;
        }
        /// <summary>
        /// 店面编号
        /// </summary>
        public string Dmbh
        {
            get;
            set;
        }
        /// <summary>
        /// 店面名称
        /// </summary>
        public string Dmmc
        {
            get;
            set;
        }
        /// <summary>
        /// 营业员编号
        /// </summary>
        public string Yyybh
        {
            get;
            set;
        }



        #endregion

        private static frmHycz hycz = null;
        public static frmHycz CreateInstance()
        {
            if (null == hycz)
            {
                hycz = new frmHycz();
            }
            return hycz;
        }

        void MiddleModule_eventSend(object sender, object receiver, object msg)
        {

            frmMain frmmain = sender as frmMain;

            if (null == hycz)
            {
                hycz = new frmHycz();
            }
            //数据来源 主页面
            if (null != frmmain && hycz.ToString() == receiver.ToString())
            {
                MemberInfo model = JsonConvert.DeserializeObject<MemberInfo>(msg.ToString());
                if (model != null)
                {
                    Kh = model.Hykh;//会员卡号
                    Dmbh = model.Dmbh;//店面编号
                    Dmmc =model.Dmmc;//店面名称
                    Yyybh =model.Yyymc;//营业员名称
                    this.ctxtKh.Text = Kh;
                }
            }
        }

        private void ctxtCzje_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void ctxtZsje_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void ctxtCzje_TextChanged(object sender, EventArgs e)
        {
            string czje = this.ctxtCzje.Text.Trim();
            string zsje = this.ctxtZsje.Text.Trim();
            decimal cz = 0;
            decimal zs = 0;

            if (czje != "")
            {
                cz = decimal.Parse(czje);
            }
            if (zsje != "")
            {
                zs = decimal.Parse(zsje);
            }

            this.ctxtHj.Text = (cz + zs).ToString();
        }

        private void ctxtZsje_TextChanged(object sender, EventArgs e)
        {
            string czje = this.ctxtCzje.Text.Trim();
            string zsje = this.ctxtZsje.Text.Trim();
            decimal cz = 0;
            decimal zs = 0;

            if (czje != "")
            {
                cz = decimal.Parse(czje);
            }
            if (zsje != "")
            {
                zs = decimal.Parse(zsje);
            }

            this.ctxtHj.Text = (cz + zs).ToString();
        }

        private void gbtnCz_Click(object sender, EventArgs e)
        {
            string hj = this.ctxtHj.Text;

            if (ValidationHelper.IsNumeric(hj))
            {
                decimal je = decimal.Parse(hj);
                HykblHBLL hykblHBLL = new HykblHBLL(config.WebApi);
                //1.调用webApi接口
                int ss = hykblHBLL.HykCZByWebApi(Kh, je, Dmbh, Yyybh);
                if (ss > 0)
                {
                    if (this.checkBox1.Checked)
                    {
                        POSPrintDocument posPrintDocument = new POSPrintDocument();
                        posPrintDocument.PrintXP(Dmmc, Kh, Yyybh, this.ctxtCzje.Text, this.ctxtZsje.Text.Trim());
                    }


                    Message.MsgBoxInfo("操作成功！");
                    hycz = null;
                    this.Close();
                }


                else
                {
                    Message.MsgBoxInfo("操作失败，请联系管理员！");
                }
            }
        }


    }
}
